#pragma once
#include<assert.h>
#include<string.h>
#include<iostream>
using namespace std;
namespace buluo
{
	//char* strcpy(char* dest, const char* src)
	//{
	//	char* ret = dest;
	//	while(*dest++=*src++)
	//	{

	//	}
	//	while (*src != '\0')
	//	{
	//		*dest = *src;
	//		dest++;
	//		src++;
	//	}
	//	*dest = '\0';
	//	return ret;
	//	
	//}
	class string
	{
		friend ostream& operator<<(ostream& _cout, string& s);
	public:
		typedef char* iterator;
		typedef const char* const_iterator;
		iterator begin()
		{
			return _str;
		}
		iterator end()
		{
			return _str + _size;
		}
		const_iterator begin() const
		{
			return _str;
		}
		const_iterator end() const
		{
			return _str + _size;
		}
		//string()
		//	: _str(new char[1])
		//	, _capacity(0)
		//	, _size(0)
		//{
		//	_str[0] = '\0';
		//}
		string(const char* str = "")
			:_size(strlen(str))
		{
			_capacity = _size = 0 ? 3: _size;
			_str = new char[_capacity + 1];
			strcpy(_str, str);
		}
		string(const string& s)
			:_size(s._size)
			,_capacity(s._capacity)
		{
			_str = new char[_capacity + 1];
			strcpy(_str, s._str);
		}
		string& operator=(const string& s)
		{
			if (this != &s)
			{
				char* tmp = new char[s._capacity + 1];
				strcpy(tmp, s._str);
				delete[] _str;
				_str = tmp;
				_size = s._size;
				_capacity = s._capacity;
			}

			return *this;

		}
		char* c_str()
		{
			return _str;
		}

		const char& operator[](size_t pos) const
		{
			assert(pos < _size);
			return _str[pos];
		}

		char& operator[](size_t pos)
		{
			assert(pos < _size);
			return _str[pos];
		}

		size_t size() const
		{
			return _size;
		}

		void reserve(size_t n)
		{
			_capacity = n;
			char* tmp = new char[_capacity + 1];
			strcpy(tmp , _str);
			delete[] _str;
			_str = tmp;
		}

		void push_back(char ch)
		{
			if (_size == _capacity)
			{
				reserve(_capacity * 2);
			}
			_str[_size++] = ch;
			_str[_size] = '\0';
		}
		bool operator==(string& s)
		{
			return strcmp(_str, s._str) == 0;
		}
		bool operator!=(string& s)
		{
			return !(strcmp(_str, s._str) == 0);
		}
		bool operator>(string& s)
		{
			return strcmp(_str, s._str) > 0;
		}
		bool operator<(string& s)
		{
			return strcmp(_str, s._str) < 0;
		}
		bool operator>=(string& s)
		{
			return (*this > s) || (*this == s);
		}
		bool operator<=(string& s)
		{
			return (*this < s) || (*this == s);
		}
		
		void append(const char* str)
		{
			size_t len = strlen(str);
			if (_size + len > _capacity)
			{
				reserve(_capacity + len);
			}
			strcpy(_str + _size, str);
			_size += len;
		}
		string& operator+=(char ch)
		{
			push_back(ch);
			return *this;
		}
		string& operator+=(const char* str)
		{
			append(str);
			return *this;
		}

		void insert(char ch, size_t pos)
		{
			if (_size + 1 > _capacity)
			{
				reserve(_capacity * 2);
			}
			size_t end = _size;
			while (end >= pos)
			{
				_str[end + 1] = _str[end];
				if (end == 0)
				{
					break;
				}
				end--;
			}
			_str[pos] = ch;
			_size++;
		}
		void insert(size_t pos, const char* str)
		{
			size_t len = strlen(str);
			if (_size + len > _capacity)
			{
				reserve(_size + len);
			}
			int end = _size;
			while (end >= pos)
			{
				_str[end + len] = _str[end];
				if (end == 0)
				{
					break;
				}
				end--;
			}
			for (int i = 0; i < len; i++)
			{
				_str[pos + i] = str[i];
			}
			_size += len;
		}
		~string()
		{
			delete[] _str;
			_capacity = _size = 0;
		}
	private:
		char* _str;
		size_t _size;
		size_t _capacity;
		static size_t npos;
	};
	size_t string::npos = -1;
	ostream& operator<<(ostream& _cout, string& s)
	{
		_cout << s._str;
		return _cout;
	}
	void test()
	{
		string s;
		string s1("hello world");
		string s2(s1);
		cout << s2 << endl;
		s = s2;
		cout << s << endl;
		//string::iterator it = s.begin();
		//for (int i = 0; i < s.size(); i++)
		//{
		//	cout << s[i] << ' ';
		//}
		//for (auto& e : s)
		//{
		//	cout << e << ' ';
		//}
		s1.insert(0, "hhhhhh");
		cout << s1 << endl;
		//cout << s1 << endl;
		//s1 += ' ';
		//s1 += "xxxxxxx";
		//cout << s1 << endl;
		//s1.insert('2', 0);
		//cout << s1 << endl;
	}
}


